<?php

/**
 * @author Watanabex
 * @copyright 2011
 * Video DAO class
 */
//require('classes/utils/DBFunctions.php');
require('to/Video.php');

class videosDAO{
	const SEL_LAST_20 = "SELECT * FROM videos WHERE video_number <> '' ORDER BY fechaPublicacion DESC LIMIT 20";
	const SEL_MAIN_VIDEO = "SELECT * FROM videos WHERE id = %d";
	const SEL_TOP3 = "SELECT * FROM videos WHERE video_number <> '' ORDER BY fechaPublicacion DESC LIMIT 3";
	const SEL_TOP3_SEXYNOTAS = "SELECT * FROM videos WHERE tipo = 'sexynotas' ORDER BY fechaPublicacion DESC LIMIT 3";
	const SEL_TOP3_YOUTUBE = "SELECT * FROM videos WHERE tipo = 'youtube' ORDER BY fechaPublicacion DESC LIMIT 3";	
	const INS_VIDEO = "INSERT INTO (link, video_number, tipo, fechaPublicacion, titulo) VALUES(%s)";
	
	/*This function gets the last 20 published videos
	 * @param none
	 * @return array with Video Objects
	 * */
	public static function getLast20(){
		$sSql = self::SEL_LAST_20;
		
		$DB = DBFunctions::getInstance();		
		$arrVideos = $DB->execSelect($sSql, array());

		return self::populate($arrVideos);
	}
	
	/*This function gets the main video for /videos/
	 * @param int videoID
	 * @return array with Video Object
	 * */
	public static function getMainVideo($videoID){
		$sSql = self::SEL_MAIN_VIDEO;
		
		$DB = DBFunctions::getInstance();		
		$arrVideos = $DB->execSelect($sSql, array($videoID));

		return self::populate($arrVideos);
	}
	
	/*This function gets the top3 videos
	 * @param none
	 * @return array with Video Object
	 * */
	public static function getTop3Videos(){
		$sSql = self::SEL_TOP3;
		
		$DB = DBFunctions::getInstance();		
		$arrVideos = $DB->execSelect($sSql, array());

		return self::populate($arrVideos);
	}
	
	/*This function gets the Top 3 sexynotas videos
	 * @param none
	 * @returns array with Video objects
	*/
	public static function getTop3SexyNotas(){
		$sSql = self::SEL_TOP3_SEXYNOTAS;

		$DB = DBFunctions::getInstance();		
		$arrVideos = $DB->execSelect($sSql, array());
		
		return self::populate($arrVideos);
	}
	
	/*This function gets the Top 3 youtube videos
	 * @param none
	 * @returns array with Video objects
	*/
	public static function getTop3Youtube(){
		$sSql = self::SEL_TOP3_YOUTUBE; //$this->SEL_TOP3_YOUTUBE;
		
		$DB = DBFunctions::getInstance();
		$arrVideos = $DB->execSelect($sSql, array());
		
		return self::populate($arrVideos);
	}
	
	/*This function adds one video to the database
	 * @params string (link, video_number, tipo)
	 * @returns int number of affected rows
	*/
	public static function addVideo($sLink, $sTipo, $sVideoNumber, $sTitulo){
		$sParams = "'{$sLink}', '{$sVideoNumber}', '{$sTipo}', '".date("Y-m-d H:i:s")."', {$sTitulo}";
		
		$sSQL = self::INS_VIDEO;
		
		$DB = DBFunctions::getInstance();
		
		//$numRows = $DB->displayQ($sSql, array($sParams));
		$numRows = $DB->execQ($sSQL, array($sParams));
		
		return $numRows;
	}
	
	/*This function get's the result array and builds a video array object
	 * @param array Video result
	 * @returns array with Video objects */
	public static function populate($arrVideos){
		
		$objVideos = array();
		
		foreach($arrVideos as $V){
			$oVideo = new Video();
			$oVideo->setID($V["id"]);
			$oVideo->setLink($V["link"]);
			$oVideo->setTipo($V["tipo"]);
			$oVideo->setVideoNumber($V["video_number"]);
			$oVideo->setFechaPublicacion($V["fechaPublicacion"]);
			$oVideo->setTitulo($V["titulo"]);
			$oVideo->setThumbnail($V["thumbnail"]);
			array_push($objVideos, $oVideo);
		}
		return $objVideos;
	}
	
	
}